package cn.mcch.investigator.presenter.impl;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.mcch.investigator.R;
import cn.mcch.investigator.adapter.base.CommonAdapter;
import cn.mcch.investigator.adapter.base.ViewHolder;
import cn.mcch.investigator.customview.expandablelayout.ExpandableLinearLayout;
import cn.mcch.investigator.customview.pulltorefresh.PullToRefreshLayout;
import cn.mcch.investigator.listener.OnResponseListener;
import cn.mcch.investigator.model.bean.Order;
import cn.mcch.investigator.model.impl.FindModelImpl;
import cn.mcch.investigator.presenter.BasePresenter;
import cn.mcch.investigator.presenter.IFindPresenter;
import cn.mcch.investigator.utils.ConstantUtil;
import cn.mcch.investigator.utils.JsonUtils;
import cn.mcch.investigator.utils.ToastUtils;
import cn.mcch.investigator.viewdelegate.impl.FindViewDelegateImpl;

/**
 * Created by Administrator on 2016/12/17.
 */
public class FindPresenterImpl extends BasePresenter<FindViewDelegateImpl, FindModelImpl> implements IFindPresenter {
    @Override
    public Class<FindViewDelegateImpl> getViewDelegateClass() {
        return FindViewDelegateImpl.class;
    }

    @Override
    public Class<FindModelImpl> getModelClass() {
        return FindModelImpl.class;
    }

    @Override
    public void find(int id, OnResponseListener l) {
        Map<String, String> params = new LinkedHashMap<>();
        params.put("keyw",mViewDelegate.getText(R.id.se_find_key));
        mModel.find(id, ConstantUtil.ACT_ORDER_SEARCH, params, l);
    }

    @Override
    public boolean verifyInput() {

        if (TextUtils.isEmpty(mViewDelegate.getText(R.id.se_find_key))) {
            ToastUtils.showToast("请输入报案号或车牌进行查询");
            return false;
        }

        return true;
    }


    public void toggle() {
        ExpandableLinearLayout ell = (ExpandableLinearLayout) mViewDelegate.findViewById(R.id.ell_list_item);
        mViewDelegate.setImageResource(R.id.iv_arrow,ell.isExpanded()? R.drawable.arrow_down: R.drawable.arrow_up);
        ell.toggle();
    }

    public void setVisibility(boolean result) {
        mViewDelegate.setVisibility(R.id.ell_list_item, result).setVisibility(R.id.iv_arrow, result);
    }

    public void setData(String json) {
        Order item = JsonUtils.fromJsonToObject(json, Order.class);
        mViewDelegate.setText(R.id.tv_order_no, item.sn).setText(R.id.tv_customer_car, item.no).setText(R.id.tv_order_attr, item.attr)
                .setText(R.id.tv_order_type, item.type)
                .setText(R.id.tv_customer_car_type, item.car)
                .setText(R.id.tv_customer, item.name)
                .setText(R.id.tv_customer_phone, item.tel)
                .setText(R.id.tv_order_area, item.area);
        // 推修单位
        mViewDelegate.setVisibility(R.id.tv_order_txdw, TextUtils.isEmpty(item.garage_name) ? false : true);
        mViewDelegate.setText(R.id.tv_order_txdw, item.garage_name);
        // 救援公司名称
        mViewDelegate.setVisibility(R.id.tv_rescue_company, TextUtils.isEmpty(item.com_name) ? false : true);
        mViewDelegate.setText(R.id.tv_rescue_company, item.com_name);
        // 救援人员
        mViewDelegate.setVisibility(R.id.tv_driver, TextUtils.isEmpty(item.driver_name) ? false : true);
        mViewDelegate.setText(R.id.tv_driver, item.driver_name);
        // 救援人员联系方式
        mViewDelegate.setVisibility(R.id.tv_driver_phone, TextUtils.isEmpty(item.driver_tel) ? false : true);
        mViewDelegate.setText(R.id.tv_driver_phone, item.driver_tel);
        // 救援人员车牌号
        mViewDelegate.setVisibility(R.id.tv_driver_car, TextUtils.isEmpty(item.driver_no) ? false : true);
        mViewDelegate.setText(R.id.tv_driver_car, item.driver_no);
        // 救援人员车型
        mViewDelegate.setVisibility(R.id.tv_driver_car_type, TextUtils.isEmpty(item.driver_type) ? false : true);
        mViewDelegate.setText(R.id.tv_driver_car_type, item.driver_type);
        // 录入时间
        mViewDelegate.setVisibility(R.id.tv_lrsj, TextUtils.isEmpty(item.addtime) ? false : true);
        mViewDelegate.setText(R.id.tv_lrsj, item.addtime);
        // 推修时间
        mViewDelegate.setVisibility(R.id.tv_txsj, TextUtils.isEmpty(item.recommend_time) ? false : true);
        mViewDelegate.setText(R.id.tv_txsj, item.recommend_time);
        // 退案件状态
        mViewDelegate.setVisibility(R.id.tv_order_status, TextUtils.isEmpty(item.status) ? false : true);
        mViewDelegate.setText(R.id.tv_order_status, item.status);
    }
}
